package com.restaurant.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.restaurant.domain.Inventory;
import com.restaurant.domain.Menu;
import com.restaurant.domain.Menuinventory;
import com.restaurant.service.InventoryService;
import com.restaurant.service.MenuService;
import com.restaurant.service.MenuinventoryService;

@Component
@RequestMapping("/action/menuinventory.do")
@SessionAttributes("menu")
public class MenuInventoryFormController {
	
	private final String loadAllMenu = "from Menu where isdeleted = false";
	
	@Autowired
	private MenuinventoryService objMenuinventoryService;
	
	@Autowired
	private MenuService objMenuService;
	
	@Autowired
	private InventoryService objInventoryService;
	
	@RequestMapping(method=RequestMethod.GET)
	public String menuInventoryGETRequest(ModelMap map){
		ArrayList<Menu> listMenus = null;
		Menu menu = new Menu();
		try {
			listMenus = (ArrayList<Menu>) objMenuService.loadByHql(loadAllMenu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("listMenus", listMenus);
		map.addAttribute("menu", menu);
		return "MenuInventory";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String menuInventoryPOSTRequest(ModelMap map, @ModelAttribute ArrayList<Menu> listMenus, @ModelAttribute Menu menu, BindingResult result, HttpServletRequest request, HttpServletResponse response){
		String action = request.getParameter("action");
		
		if(action.compareTo("onselect")==0){
			if(menu.getMenuID()!=0)
				map = onSelectMenu(map, menu, request, response);
			else
				return "redirect:/menuinventory.do";
		}else if(action.compareTo("edit")==0){
			map = onAddMenuInventory(map, menu, request, response);
		}else if(action.compareTo("del")==0){
			map = onDeleteMenuInventory(map, menu, request, response);
		}
		
		try {
			listMenus = (ArrayList<Menu>) objMenuService.loadByHql(loadAllMenu);
		} catch (Exception e) {
			e.printStackTrace();
		}
		map.addAttribute("listMenus", listMenus);
		return "MenuInventory";
	}
	
	private ModelMap onSelectMenu(ModelMap map, Menu menu, HttpServletRequest request, HttpServletResponse response){
		String menuID = menu.getMenuID().toString();
		
		String queryLoadInventoryForMenuID = "from Menuinventory where menu.menuID = " + menuID;
		
		ArrayList<Menuinventory> listMenuinventorys = null;
		try {
			listMenuinventorys = (ArrayList<Menuinventory>) objMenuinventoryService.loadByHql(queryLoadInventoryForMenuID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		StringBuffer inventoryIDs = new StringBuffer("(");
		for(int i=0;i<listMenuinventorys.size();i++){
			inventoryIDs.append(listMenuinventorys.get(i).getInventory().getIdinventoryID());
			inventoryIDs.append(",");
		}
		
		if(inventoryIDs.lastIndexOf(",")>0)
			inventoryIDs.deleteCharAt(inventoryIDs.lastIndexOf(","));
		else
			inventoryIDs.append("-1");
		inventoryIDs.append(")");
		
		String queryNotMenuInventory = "from Inventory where isdeleted = false and idinventoryID not in " + inventoryIDs ;
		
		ArrayList<Inventory> listNotMenuInventory = null;
		
		try {
			listNotMenuInventory = (ArrayList<Inventory>) objInventoryService.loadByHql(queryNotMenuInventory);
		} catch (Exception e) {
			e.printStackTrace();	
		}
		
		
				
		map.addAttribute("listMenuinventorys", listMenuinventorys);
		map.addAttribute("listNotMenuInventory", listNotMenuInventory);
		map.addAttribute("menu", menu);
		
		return map;
	}
	
	private ModelMap onAddMenuInventory (ModelMap map, Menu menu, HttpServletRequest request, HttpServletResponse response){
		Long menuID = menu.getMenuID();
		
		StringBuffer ids = new StringBuffer(request.getParameter("ids"));
		ids.deleteCharAt(ids.lastIndexOf(","));
		ids.insert(0, "(");
		ids.insert(ids.length(), ")");
		
		ArrayList<Inventory> listInventories = null;
		try {
			listInventories = (ArrayList<Inventory>) objInventoryService.loadByHql("from Inventory where idinventoryID in " + ids.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Menuinventory menuinventory = null;
		
		for (Inventory inventory : listInventories) {
			menuinventory = new Menuinventory();
			menuinventory.setIsdeleted(false);
			menuinventory.setInventory(inventory);
			try {
				menuinventory.setMenu(objMenuService.loadByPrimaryKey(menuID));
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				objMenuinventoryService.addOrModify(menuinventory);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return onSelectMenu(map, menu, request, response);
	}
	
	private ModelMap onDeleteMenuInventory(ModelMap map, Menu menu, HttpServletRequest request, HttpServletResponse response){
		
		StringBuffer ids = new StringBuffer(request.getParameter("ids"));
		ids.deleteCharAt(ids.lastIndexOf(","));
		ids.insert(0, "(");
		ids.insert(ids.length(), ")");
		
		ArrayList<Menuinventory> listMenuinventories = null;
		
		try {
			listMenuinventories = (ArrayList<Menuinventory>) objMenuinventoryService.loadByHql("from Menuinventory where menuinventoryID in " + ids);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
		for (Menuinventory menuinventory : listMenuinventories) {
			try {
				objMenuinventoryService.deleteRecord(menuinventory);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return onSelectMenu(map, menu, request, response);
	}
}
